arnt beAddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create("sent_spaceanon_nullenergyfield")
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Initialize()
	self.Entity:SetModel("models/Combine_Helicopter/helicopter_bomb01.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableMotion(false)
	end
end

function ENT:Think()
	// Begin effect code
	local lightningStart = nil
	local lightningEnd = nil
	for i=1,math.random(2,4) do	
		local pos = self.Entity:GetPos()+Vector(math.random(-1512,1512),math.random(-1512,1512),math.random(-1512,1512))
		spark = ents.Create("point_tesla")
		spark:SetKeyValue("targetname", "teslab")
		spark:SetKeyValue("m_SoundName" ,"DoSpark")
		spark:SetKeyValue("texture" ,"sprites/plasma.spr")
		spark:SetKeyValue("m_Color" , math.random(0,255).." "..math.random(0,255).." "..150+math.random(0,100))
		spark:SetKeyValue("m_flRadius" , "250")
		spark:SetKeyValue("beamcount_min", "5")
		spark:SetKeyValue("beamcount_max", "30")
		spark:SetKeyValue("thick_min", "15")
		spark:SetKeyValue("thick_max", "40")
		spark:SetKeyValue("lifetime_min" ,"0.1")
		spark:SetKeyValue("lifetime_max", "0.2")
		spark:SetKeyValue("interval_min", "0.05")
		spark:SetKeyValue("interval_max" ,"0.08")
		spark:SetPos(pos)
		spark:Spawn()
		spark:Fire("DoSpark","",0)
		spark:Fire("kill","", .25)
//		spark:EmitSound("ambient/atmosphere/thunder"..math.random(1,4)..".wav", 500, 100) --Removed this for now.
		if i == 1 then
			lightningStart = pos
		else
			lightningEnd = pos
		end
	end
	self:MakeLightning(lightningStart,lightningEnd)
	//end effect code
	nebula = ents.FindInSphere(self.Entity:GetPos(),2048)
	for _,v in pairs (nebula) do
		if v:IsValid() then
			local str = v:GetClass()
			if (not string.find(str, "physgun_beam")) and
				(not string.find(str, "predicted_viewmodel")) and
				(not (string.find(str, "func_") and (str ~= "func_physbox"))) and
				(not string.find(str, "info_")) and
				(not string.find(str, "point_")) and
				(not string.find(str, "path_")) and
				(not string.find(str, "node")) and
				(not string.find(str, "sent_spaceanon")) then
				if string.find(str, "storage") or string.find(str, "energy") then
					local power =  v:GetResourceAmount("energy")
					self:IonDischarge(v)
					if power > 500 then
						v:ConsumeResource("energy", 500)
					else
						v:ConsumeResource("energy", power)
					end
				end
				local vel = v:GetVelocity()
				local dir = vel:Normalize()
				local amp = math.Clamp(vel:Length(),0,50)
				local phys = v:GetPhysicsObject()
				if phys and phys:IsValid() then
					phys:SetVelocity(dir*amp)
				end
			end
		end
	end
	self.Entity:NextThink(CurTime()+3)
end

function ENT:IonDischarge(ent)
	local pos = self.Entity:GetPos()+Vector(math.random(-1024,1024), math.random(-1024,1024), math.random(-1024,1024))
	local id = math.random(1,999999999)
	local gID = "ionguider"..id
	spark = ents.Create("point_tesla")
	spark:SetKeyValue("targetname", gID)
	spark:SetKeyValue("m_SoundName" ,"DoSpark")
	spark:SetKeyValue("texture" ,"sprites/plasma.spr")
	spark:SetKeyValue("m_Color" ,"255 0 0")
	spark:SetKeyValue("m_flRadius" , "250")
	spark:SetKeyValue("beamcount_min", "5")
	spark:SetKeyValue("beamcount_max", "30")
	spark:SetKeyValue("thick_min", "15")
	spark:SetKeyValue("thick_max", "40")
	spark:SetKeyValue("lifetime_min" ,"0.6")
	spark:SetKeyValue("lifetime_max", ".7")
	spark:SetKeyValue("interval_min", "0.05")
	spark:SetKeyValue("interval_max" ,"0.08")
	spark:SetPos(pos)
	spark:Spawn()
	spark:Fire("DoSpark","",0)
	spark:Fire("kill","", 2)
	
	local tID = "iontarg"..id
	local targ = ents.Create("info_target")
	targ:SetKeyValue("targetname", tID)
	targ:SetPos(ent:GetPos())
	targ:SetParent(ent)
	targ:Spawn()
	targ:Fire("kill","", .7)
	
	IonBeam = ents.Create("env_laser")
	IonBeam:SetPos(spark:GetPos())
	IonBeam:SetKeyValue("renderamt", "255")
	IonBeam:SetKeyValue("rendercolor", "100 0 255")
	IonBeam:SetKeyValue("texture", "cable/physbeam.vmt")
	IonBeam:SetKeyValue("TextureScroll", "-20")
	IonBeam:SetKeyValue("targetname", tID)
	IonBeam:SetKeyValue("damage", "50")
	IonBeam:SetKeyValue("width", "10")
	IonBeam:SetKeyValue("dissolvetype", "1")
	IonBeam:SetKeyValue("NoiseAmplitude", "5")
	IonBeam:SetKeyValue("LaserTarget", tID)
	IonBeam:Spawn()
	IonBeam:Fire("TurnOn","", 0)
	IonBeam:Fire("kill","", .5)
end

function ENT:MakeLightning(startpos,endpos) -- Added this in to make a area of effect
	local beam = ents.Create("env_beam")
	beam:SetKeyValue("texture", "cable/physbeam.vmt")
	beam:SetKeyValue("BoltWidth","10")
	beam:SetKeyValue("damage","300")
	beam:SetKeyValue("life","0.2")
	beam:SetKeyValue("NoiseAmplitude","5")

	local index = tostring(beam:EntIndex())

	beam.startent = ents.Create("info_target")
	beam.startent:SetPos(startpos)
	beam.startent:SetName("weatherlightningstart"..index)
	beam.startent:Spawn()

	beam.endent = ents.Create("info_target")
	beam.endent:SetPos(endpos)
	beam.endent:SetName("weatherlightningend"..index)
	beam.endent:Spawn()
	 
	beam:SetKeyValue("LightningStart","weatherlightningstart"..index)
	beam:SetKeyValue("LightningEnd","weatherlightningend"..index)
	beam:Spawn()
	beam:Fire("StrikeOnce","",0)
	timer.Simple(0.2,self.DeleteLightning,self,beam)
end
 
function ENT:DeleteLightning(beam) --doesn't work well
	if beam.startent:IsValid() then
		beam.startent:Remove()
	end
	if beam.endent:IsValid() then
		beam.endent:Remove()
	end
	if beam:IsValid() then
		beam:Remove()
	end
	if beam.startent:IsValid() then
		beam.startent:Fire("kill","","0")
	end
	if beam.endent:IsValid() then
		beam.endent:Fire("kill","","0")
	end
	if beam:IsValid() then
		beam:Fire("kill","","0")
	end
end 
